package
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class BFmap
	{
		//地图参数
		private var m_iMapWidth:Number = 4096;
		private var m_iMapHeight:Number = 4096;
		private var m_iViewWidth:Number = 3800;
		private var m_iViewHeight:Number = 3800;
		private var m_pMap:Sprite = new Sprite;
		//资源库
		public var s_aGunArray:Array = new Array;
		public var s_aBodyArray:Array = new Array;
		//
		public var s_aPlayerArray:Array = new Array;
		public var s_aTankArray:Array = new Array;
		public var s_aFlyerArray:Array = new Array;
		//所谓的本地玩家
		public var localPlayer:Player = new Player("local",127001);
		//
		public function BFmap()
		{
		}
		//
		public function LoadXML( xml:XML ):void{//读取地图设置
			//地图大小等
			m_iMapWidth = xml.@mapWidth;
			m_iMapHeight= xml.@mapHeight;
			m_iViewWidth= xml.@viewWidth;
			m_iViewHeight=xml.@viewHeight;
			//地形
			//单位索引
			var str:String = "";
			var load:URLLoader = new URLLoader;
			load.addEventListener(Event.COMPLETE,LoadTankBmp);
			//load.dataFormat=DataFormat.Binary;
			load.load(new URLRequest("tank.xml"));
				//载入所有单位用图片
			//脚本
				//生成触发器
		}
		//
		public function InitMap():void{
			//绘制地表
			m_pMap.graphics.lineStyle(1,0,0);
			m_pMap.graphics.beginBitmapFill( new BitmapData(32,32), null, true, true );//用基本纹理填充地表
			m_pMap.graphics.drawRect(-m_iMapWidth,-m_iMapHeight,m_iMapWidth*2,m_iMapHeight*2);
			m_pMap.graphics.endFill();
			//添加装饰物
			//添加中立建筑和单位等
			//脚本
			//预置单位
			//触发
			AddPlayer(localPlayer);
		}
		//
		public function LoadTankBmp( e:Event ):void{
			var xml:XML = new XML(e.currentTarget.data);//能转化为XML吗?
			for each( var x:XML in xml.children() ){
				if(x.name()=="TankBody"){
					var b:TankBody = new TankBody(x);
					s_aBodyArray.push(b);
				}
				else if(x.name()=="TankGun"){
					var g:TankGun = new TankGun(x);
					s_aGunArray.push(g);
				}
			}
		}
		//
		public function Updata():void{
			//没有下载完,返回
			if(rLoader.loading){
				Loading();
				return;
			}
			Refresh();
		}
		public function Loading():void{
		}
		public function Refresh():void{
			for( var i:int=s_aFlyerArray.length-1; i>=0; i--){
				var f:Flyer = s_aFlyerArray[i];
				f.Updata();
				for each( var o:MomoTank in s_aTankArray ){
					if( f.master!=o && o.checkObject(f) ){	//碰到坦克了(不能打到自己)
						s_aFlyerArray.splice(i,1);	//移除子弹
						//创建爆炸效果或跳弹效果等
						//var d:int = 1;
						//if(f.direct==o.direct)d=3;//背面
						//if(Math.abs(fly.direct-o.direct)==2)d=0;//正面
						//var v:int = uint( fly.atk*fly.brk/o.defend(d) );
						//o.Hurt(v);//计算伤害
						//CreateWaveText(v.toString(),fly.x,fly.y);
					}
				}
			}
			for( var i:int=s_aTankArray.length-1; i>=0; i--){
				var m:MomoTank = s_aTankArray[i];
				m.Updata();
			}
			
		}
		//
		public function AddPlayer( p:Player ):void{
			s_aPlayerArray.push(p);
			p.viewPoint().x=0;//最好像CS那样随机蹦几个点直到玩家选择阵营(德军 苏军 观察者)
			p.viewPoint().y=0;
			JoinForce( p,0 );//直接设为0了……观察者,本来应该提供一个选择界面
		}
		//
		public function JoinForce( p:Player, force:int ):void{
			p.forceID=force;//如果是观察者阵营就跳出
			//原本应该是提供出击界面,即选择自己的车辆和其他附件等,此处直接给1个坦克
			p.tank = CreateTank( 0, p.viewPoint().x+320, p.viewPoint().y+200, -90, p );
			//此处应该使用专门的函数创建坦克,由函数负责把新坦克加入到场景中
		}
		//
		public function SetViewPoint( p:Point ):void{
			
		}
		//
		public function CreateTank( type:int, x:Number, y:Number, r:Number, who:Player ):MomoTank{
			var t:MomoTank = new MomoTank(type,x,y,r,who);
			s_aTankArray.push(t);
			return t;
		}
		//
		public function RunTrigger():void{
			//处理脚本
		}
		//
	}
}